/**
 * @author yxw
 * date : 2013年10月2日 下午6:56:51 
 */
package com.tzinfo.tzt.activity;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.Button;
import android.widget.ListView;
import android.widget.SimpleAdapter;

import com.tzinfo.tzt.config.FilterConfig;
import com.tzinfo.tzt.R;

public class FilterPageActivity extends Activity implements OnClickListener {

	private ListView mListView;
	private Button sureBtn;

	private SimpleAdapter simpleAdapter;
	private List<Map<String, Object>> data;
	private FilterConfig filterInfo = new FilterConfig();
	private static final int REQUESTCODE = 1;
	private int tabNum = 0;
	private int[] filterArray = null;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_filter);
		choseFilterItem();
		init();
	}

	private void choseFilterItem() {
		Intent thisIntent = this.getIntent();
		tabNum = thisIntent.getIntExtra("tabNum", 0);
		switch (tabNum) {
		case 0:
			filterArray = new int[] { 0, 1 };
			break;
		case 1:
			filterArray = new int[] { 0, 1 };
			break;
		case 2:
			filterArray = new int[] { 0, 2, 4, 5, 6 };
			break;
		case 3:
			filterArray = new int[] { 0, 3, 4 };
			break;
		default:
			break;
		}
	}

	public void init() {
		sureBtn = (Button) this.findViewById(R.id.bt_sure);
		sureBtn.setOnClickListener(this);
		// 设置ListView数据
		mListView = (ListView) this.findViewById(R.id.list_content);
		getData();
		simpleAdapter = new SimpleAdapter(this, data, R.layout.item_filter,
				new String[] { "filterName", "filterContent" }, new int[] {
						R.id.text_filter_name, R.id.text_filter_content });
		mListView.setAdapter(simpleAdapter);
		mListView.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
					long arg3) {
				Intent intent = new Intent(FilterPageActivity.this,
						FilterContentActivity.class);
				int filterNum = filterArray[arg2];
				intent.putExtra("filterNum", filterNum);
				intent.putExtra("tabNum", tabNum);
				// 将当前的条件传递到下一页面
				if (filterInfo.filterResult != null) {
					intent.putExtra("filterContent",
							filterInfo.filterResult[filterNum]);
				} else {
					intent.putExtra("filterContent", 0);
				}
				startActivityForResult(intent, REQUESTCODE);
			}
		});

	}

	private void getData() {
		Intent thisIntent = this.getIntent();
		// 设置第一个为区域或距离
		data = new ArrayList<Map<String, Object>>();
		initData();
	}

	private void initData() {
		Map<String, Object> map;
		for (int i = 0; i < filterArray.length; i++) {
			int filterNum = filterArray[i];
			map = new HashMap<String, Object>();
			map.put("filterName", FilterConfig.FILTER_NAMES[filterNum]);
			if (filterInfo.filterResult != null) {
				map.put("filterContent",
						FilterConfig.FILTER_CONTENT[filterNum][filterInfo.filterResult[filterNum]]);
			} else {
				map.put("filterContent",
						FilterConfig.FILTER_CONTENT[filterNum][0]);
			}
			data.add(map);
		}
	}

	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		// TODO Auto-generated method stub
		super.onActivityResult(requestCode, resultCode, data);
		if (requestCode == REQUESTCODE) {
			if (resultCode == 2) {
				update(data);
			}
		}
	}

	private void update(Intent intent) {
		int filterNum = intent.getIntExtra("filterNum", 0);
		int filterContent = intent.getIntExtra("filterContent", 0);
		filterInfo.filterResult[filterNum] = filterContent;
		data.clear();
		initData();
		simpleAdapter.notifyDataSetChanged();
	}

	// 不使用监听是因为在布局文件中已经定义
	public void back(View view) {
		this.finish();
	}

	// 不使用监听是因为在布局文件中已经定义
	public void reset(View view) {
		filterInfo.reset();
		data.clear();
		initData();
		simpleAdapter.notifyDataSetChanged();
	}

	@Override
	public void onClick(View view) {
		switch (view.getId()) {
		case R.id.bt_sure:
			// 点击确定
			switch (tabNum) {
			case 2://二手房
				Intent intent2 = new Intent(this, FilterOldHouseResultActivity.class);
				intent2.putExtra("filterResult", filterInfo.filterResult);
				intent2.putExtra("tabNum", tabNum);
				startActivity(intent2);
				break;
			case 3://租房
				Intent intent3 = new Intent(this, FilterRentHouseResultActivity.class);
				intent3.putExtra("filterResult", filterInfo.filterResult);
				intent3.putExtra("tabNum", tabNum);
				startActivity(intent3);
				break;

			default:
				Intent intent = new Intent(this, FilterNewHouseResultActivity.class);
				intent.putExtra("filterResult", filterInfo.filterResult);
				intent.putExtra("tabNum", tabNum);
				startActivity(intent);
				break;
			}
			
			break;

		default:
			break;
		}

	}

}
